package com.huilian.iotbox.admin.wap.controller;

import com.huilian.iotbox.admin.wap.service.DisplayRackService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.DisplayRackDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chenqiwen
 * @CreateDate: 2022-01-14-18-29
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/api/displayRack")
public class DisplayRackController extends UserInfoController {
    @Autowired
    private DisplayRackService displayRackService;


    @PostMapping("/findDisplayByDeviceIdAndHierarchy")
    public ServerResponse findDisplayByDeviceIdAndHierarchy(@RequestBody DisplayRackDto displayRackDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        displayRackDto.setUserInfo(userInfo);
        displayRackDto.setMerchantStoreId(userInfo.getMchStoreId());
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            displayRackDto.setUserId(userInfo.getParentId());
        } else {
            displayRackDto.setUserId(userInfo.getId());
        }
        try {
            return ServerResponse.createBySuccess(displayRackService.findDisplayByDeviceIdAndHierarchy(displayRackDto));
        } catch (Exception e) {
            log.error("查找货道信息失败！" + e.getMessage() );
            return ServerResponse.createByError(e.getMessage());
        }
    }
    @PostMapping("/getHierarchyByDeviceId")
    public ServerResponse getHierarchyByDeviceId(@RequestBody DisplayRackDto displayRackDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        displayRackDto.setUserInfo(userInfo);
        displayRackDto.setMerchantStoreId(userInfo.getMchStoreId());
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            displayRackDto.setUserId(userInfo.getParentId());
        } else {
            displayRackDto.setUserId(userInfo.getId());
        }
        try {
            return ServerResponse.createBySuccess(displayRackService.getHierarchyByDeviceId(displayRackDto));
        } catch (Exception e) {
            log.error("查找货道信息失败！" + e.getMessage() );
            return ServerResponse.createByError(e.getMessage());
        }
    }
    @PostMapping("/findDisplayByDeviceId")
    public ServerResponse findDisplayByDeviceId(@RequestBody DisplayRackDto displayRackDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        displayRackDto.setUserInfo(userInfo);
        displayRackDto.setMerchantStoreId(userInfo.getMchStoreId());
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            displayRackDto.setUserId(userInfo.getParentId());
        } else {
            displayRackDto.setUserId(userInfo.getId());
        }
        try {
            return ServerResponse.createBySuccess(displayRackService.findDisplayByDeviceId(displayRackDto));
        } catch (Exception e) {
            log.error("查找货道信息失败！" + e.getMessage() );
            return ServerResponse.createByError(e.getMessage());
        }
    }
}
